﻿#include "QuestionBase.h"
#include "../External/gel/stxutif.h"
#include <iostream>
#include <fstream>
#include <string>
#include <time.h>

static int seed = time(NULL);

void QuestionFactory::LoadWordList()
{
	const char* filename = "data/dict.txt";
	std::wifstream fsi;
	fsi.imbue(gel::stdx::utf8_locale);

	fsi.open(filename);
	if(!fsi.good())
	{
		printf("Cannot open data file!\n");
	}

	wchar_t c;
	std::wstring s;
	std::wstring name;
	std::wstring trans;
	int wID;

	int lineIndex = 0;
	while(fsi.get(c))
	{
		// Skip BOM of UTF8
		if(c == 65279)
			continue;

		// Put all char into s
		s.push_back(c);		

		if( c==10 )
		{
			if(lineIndex%4 == 3)
			{
				PackOneWord(wID,name,trans);
				name.clear();
				trans.clear();
			}
			lineIndex ++;
		}
		else
		{
			switch(lineIndex%4)
			{
				case 0:
					{
						wID = atoi((const char*)&c);
						break;
					}
				case 1:// title line, word
					{
						name.push_back(c);
						break;
					}
				case 2:// word
					{
						trans.push_back(c)	;
						break;
					}
				case 3:// empty line
					{
						break;
					}
				default:break;
			}
		}
	}

	// Pack last word
	PackOneWord(wID,name,trans);

}

void QuestionFactory::LoadQuestionPool(QuestionPool* poolAddress)
{	
	// Load word list first
	if(m_WordList.empty())
		LoadWordList();

	for(vector<Word>::iterator it = m_WordList.begin(); it != m_WordList.end(); ++it)
	{
		Question q;
		q.m_QuestionID = it->m_WordID;
		q.m_Title = it->m_WordName;
		q.m_Answer = it->m_Trans;

		PickMixedOption(&q);

		// Insert one question
		m_QuestionList.push_back(q);
	}

	poolAddress->m_Questions = m_QuestionList;
	poolAddress->m_Size = m_QuestionList.size();
}

void QuestionFactory::PickMixedOption(Question* question)
{
	// Initialize random seed
	srand( seed);

	// Max number
	int maxRange = m_WordList.size();

	// Reset seed
	seed = rand();

	// Get a random number
	int ranNumber = rand() % maxRange;

	// If get the same word with original one
	if(wcscmp(question->m_Answer.c_str(),m_WordList[ranNumber].m_Trans.c_str()) == 0 )
	{
		// Last one
		if(ranNumber == (maxRange - 1))
		{
			maxRange -= 1;
		}
		// Other conditions
		else
		{
			ranNumber += 1;
		}
	}

	wstring mixedAnswer = m_WordList[ranNumber].m_Trans;

	// Random sort of two options.
	int answerPlace = rand() % 2;

	if(answerPlace == 0 )
	{
		question->m_Options[OptionA] = question->m_Answer;
		question->m_Options[OptionB]  = mixedAnswer;
	}
	else
	{
		question->m_Options[OptionA] = mixedAnswer;
		question->m_Options[OptionB] = question->m_Answer;
	}

}

void QuestionFactory::PackOneQuestion(int qId,wstring title,wstring answer,wstring optionA,wstring optionB)
{

}

void QuestionFactory::PackOneWord(int wId,std::wstring name,std::wstring tran)
{
	Word word;
	word.m_WordID = wId;
	word.m_WordName = name;
	word.m_Trans = tran;

	m_WordList.push_back(word);
}

